package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.dialog.viewerdialog.SaturnViewerDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnViewNode extends Shape {

	public static final String SHOW_NODE_VIEW = Messages
			.getString("SaturnViewNode.view"); //$NON-NLS-1$

	public static final String KEY_NODE_VIEW = "view";

	public static final String KEY_PROPERTY_VIEW_NODE_NAME = "_property_name";

	public static final String KEY_PROPERTY_VIEW_NODE_DESCRITPION = "_property_description";

	public static final String KEY_PROPERTY_VIEW_NODE_TYPE = "_property_type";

	public static final String KEY_PROPERTY_VIEW_NODE_LOCATION = "_property_Location";

	public static final String SHOW_VIEW_NODE_NAME = "name";

	public static final String SHOW_VIEW_NODE_DESCRITPION = "description";

	public static final String SHOW_VIEW_NODE_TYPE = "type";

	public static final String SHOW_VIEW_NODE_LOCATION = "location";

	private static final long serialVersionUID = 7400881533315897934L;

	private static int number = 1;

	private String type = SelectionCandidate.viewTypes[0];

	private String actionlocation = "";

	private Image image = Activator.getImage(Images.VIEW_ICON);

	public SaturnViewNode() {
		super.setName(SHOW_NODE_VIEW + number++);
	}

	public Image getIcon() {
		return image;
	}

	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	public String getType() {

		return type;
	}

	public void setType(String type) {

		if (!this.type.equals(type)) {
			this.type = type;
			firePropertyChange(PROPERTERY_CHANGS, null, type);
		}
	}

	public String getActionlocation() {

		return actionlocation;
	}

	public void setActionlocation(String actionlocation) {

		if (!this.actionlocation.equals(actionlocation)) {
			this.actionlocation = actionlocation;
			firePropertyChange(PROPERTERY_CHANGS, null, actionlocation);
		}
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node viewNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_VIEW, null);

		SaturnW3CUtil.createChildNode(viewNode, SHOW_VIEW_NODE_NAME, super
				.getName());

		SaturnW3CUtil.createChildNode(viewNode, SHOW_VIEW_NODE_TYPE, this.type);

		SaturnW3CUtil.createChildNode(viewNode, SHOW_VIEW_NODE_LOCATION,
				this.actionlocation);

		return viewNode;
	}

	public Dialog getDialog() {
		SaturnViewerDialog dialog = new SaturnViewerDialog(super.getShell(), this);
		return dialog;
	}

	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnViewNode saturnViewNode = new SaturnViewNode();
		SaturnViewNode saturn = (SaturnViewNode) shape;
		saturnViewNode.setLocation(mousePoint);
		saturnViewNode.setName(saturn.getName());
		saturnViewNode.setDescription(saturn.getDescription());
		saturnViewNode.setType(saturn.getType());
		saturnViewNode.setActionlocation(saturn.getActionlocation());
		saturnViewNode.setId(saturn.getId());
		saturnViewNode.setNextId(saturn.getNextId());
		return saturnViewNode;
	}

	protected void setImage(Image image) {
		this.image = image;
	}

	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
	
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace("jsp" , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "jsp",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("type", getType());
	 	infos.put("value", getActionlocation());
	 	infos.put("description", getDescription());
		
		para.setInfourl( name + "-view.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Action.ViewNode")); //$NON-NLS-1$
		para.setImagename("view");
		
		String templatePath = "template/export/action/view";
		String Path = filePath + File.separator + name + "-view.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
}
